package demo1.web.servlet.userBuyapi;

import demo1.dao.Mapper.goodsMapper;
import demo1.dao.Mapper.userBuyMapper;
import demo1.pojo.goods;
import demo1.pojo.userBuy;
import demo1.dao.sql.goodssql;
import demo1.dao.sql.userBuysql;
import demo1.service.userBuyService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/addToGouwucar")
public class addToGouwucar extends HttpServlet {//添加到购物车里面
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        int goodsID = Integer.parseInt(req.getParameter("goodsID"));
        //获取session对象
        HttpSession session = req.getSession();
        Object userObject = session.getAttribute("users");
        userBuy users = (userBuy) userObject;

        //用session对象的值
//        userBuyMapper userBuyMapper = new userBuysql();
//        goodsMapper goodsMapper = new goodssql();
//        goods good = new goods();
        try {
//            good = goodsMapper.selectByGoodsId(goodsID);//通过获取id来获取对应商品的信息
//            String goodsName = good.getGoodsName();
//            String goodsMessage = good.getGoodsMessage();
//            int price = good.getPrice();
//            //查看避免重复添加到购物车
//            if(userBuyMapper.checkGouwucar(users.getGouwuid(), goodsName,goodsMessage) == 1){
//                //如果没有该商品
//                userBuyMapper.insertGoods(users.getGouwuid(), price,goodsMessage,goodsName,0);
//            }
            userBuyService addToGouwucar = new userBuyService();
            addToGouwucar.addToGouwucar(goodsID, users.getGouwuid());
            req.getRequestDispatcher("showAllGoods").forward(req,resp);//转发到另一个界面
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
